unit UntSearch;

interface

uses DBClient, DB,StdCtrls, DBGridEh,RzEdit,MyAccess;

type
  TCustomSearch = class(TObject)
  public
    class function InitialList(AList: TComboBox; ADBGridEh: TDBGridEh): Boolean;
    class procedure Search(AList: TComboBox; AEdit: TRzEdit; AClientDataset:
        TClientDataset);
    class procedure SearchDB(AList: TComboBox; AEdit: TRzEdit; AClientDataset:
        TClientDataset; aMyQuery: TMyQuery; const tableName, orderby, condition:
        string);
  end;

implementation

uses
  MultiLang, Classes, SysUtils;

class function TCustomSearch.InitialList(AList: TComboBox; ADBGridEh: TDBGridEh): Boolean;
var
  iindex:Integer ;
begin
  Result := False;
  AList.Clear;
  AList.Items.Add(Mullang.GetMassageLang('AllItem'));
  if Assigned(ADBGridEh) then
  begin
    for iindex:=0 to ADBGridEh.FieldCount-1 do
    begin
      with ADBGridEh.Fields[iindex] do
      begin
//    ftUnknown, ,  ftDate, ftTime, ftDateTime,
//    ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
//    ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar,
//    ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob,
//    ftVariant, ftInterface, ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd);
        if DataType in [ftString, ftSmallint, ftInteger, ftWord, ftFloat, ftBoolean, ftLargeint, ftCurrency, ftBCD, ftWideString] then
        begin
          AList.Items.Add(DisplayLabel);
        end;
      end;
    end;
  end;
end;

class procedure TCustomSearch.Search(AList: TComboBox; AEdit: TRzEdit;
    AClientDataset: TClientDataset);
var
  ADisplayLabel:string;
  iindex : Integer;
begin
  if AList.ItemIndex =-1 then exit;
  if AList.ItemIndex = 0 then
  begin
    AEdit.Text :='';
    AClientDataset.Filtered := False;
    AClientDataset.Filter := '';
    exit;
  end;
  ADisplayLabel := AList.Items.Strings[AList.ItemIndex];
  for iindex :=0 to AClientDataset.FieldCount-1 do
  begin
    with AClientDataset.Fields[iindex] do
    begin
      if SameText(ADisplayLabel,DisplayLabel) then
      begin
        AClientDataset.Filtered := False;
        if DataType in [ftString,ftWideString] then
        begin
          AClientDataset.Filter := FieldName +' = '+QuotedStr(AEdit.Text);  
        end else
        begin
          AClientDataset.Filter := FieldName +' = '+AEdit.Text;
        end;
        AClientDataset.FilterOptions := [foCaseInsensitive];
        AClientDataset.Filtered := True;
        Break;
      end;
    end;
  end;
end;

class procedure TCustomSearch.SearchDB(AList: TComboBox; AEdit: TRzEdit;
    AClientDataset: TClientDataset; aMyQuery: TMyQuery; const tableName,
    orderby, condition: string);
var
  asql,ADisplayLabel:string;
  iindex :Integer;
begin
  if AList.ItemIndex =-1 then exit;
  if AList.ItemIndex = 0 then
  begin
    AEdit.Text :='';
    asql := ' select * from '+tableName+' order by '+ orderby;
  end else
  begin
    asql := ' select * from '+tableName ;
    ADisplayLabel := AList.Items.Strings[AList.ItemIndex];
    for iindex :=0 to AClientDataset.FieldCount-1 do
    begin
      with AClientDataset.Fields[iindex] do
      begin
        if SameText(ADisplayLabel,DisplayLabel) then
        begin
          if AEdit.Text='' then
          begin
            asql :=  asql + ' where '+ FieldName +'="" or  '+ FieldName +' is null' ;
            Break;
          end
          else
          if SameText(condition,'Like') then
          begin
            //if DataType in [ftString,ftWideString] then
            //begin
            asql :=  asql + ' where '+ FieldName +' like '+QuotedStr('%'+AEdit.Text +'%');
            Break;
            //end else
            //begin
            //  asql :=  asql + ' where '+ FieldName +' = '+AEdit.Text;
            //  Break;
            //end;
          end
          else
          begin
            asql :=  asql + ' where '+ FieldName +condition+QuotedStr(AEdit.Text);
            Break;
          end;

        end;
      end;
    end;
    asql := asql +' order by '+ orderby;
  end;
  AClientDataset.Close;
  with aMyQuery do
  begin
    Close;
    SQL.Clear;
    SQL.Add(asql);
    Open;
  end;
  AClientDataset.Open;
end;

end.
